package org.ccheng.xztx.service.impl;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.ccheng.xztx.service.PracticeRoomService;
import org.ccheng.xztx.util.Api;
import org.ccheng.xztx.util.ApiConstants;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 闭关修炼
 */
@Service
public class PracticeRoomServiceImpl implements PracticeRoomService {

    private static final String I = "[密室]：";

    public JSONObject getInfo() {
        JSONObject obj = JSONUtil.createObj();

        String response = Api.get(ApiConstants.API_PRACTICE_ROOM_INDEX);
        String status = ReUtil.get("(?<=状态:)\\S{1,6}(?=<br/>)|(?<=状态:)\\d+\\S+可出关(?=<)", response, 0);
        List<String> selectList = ReUtil.findAll("(?<=>)\\d+小时", response, 0);

        obj.putOpt("status", status);
        selectList.forEach(name -> {
            JSONObject date = JSONUtil.createObj();

            String practiceId = ReUtil.get(String.format("(?<=practiceId=)\\d+(?='>%s)", name), response, 0);

            date.putOpt("name", name);
            date.putOpt("practiceId", practiceId);

            obj.append("selectList", date);
        });

        return obj;
    }

    public void execute() {
        Api.out(I, "开始闭关修炼");

        JSONObject obj = getInfo();

        String status = obj.getStr("status");
        Api.out(I, status);

        if ("闭关结束".equals(status)) {
            String response = Api.get(ApiConstants.API_PRACTICE_ROOM_END_PRACTICE);

            String result = ReUtil.get("不在修炼状态", response, 0);
            if ("不在修炼状态".equals(result)) {
                Api.out(I, result);
                return;
            }

            result = ReUtil.get("你已经闭关了 \\d+分钟 .", response, 0);
            Api.out(I, result);

            result = ReUtil.get("灵气\\+\\d+", response, 0);
            Api.out(I, result);

            result = ReUtil.get("灵元\\+\\d+", response, 0);
            Api.out(I, result);

            // 刷新闭关状态
            obj = getInfo();
            status = obj.getStr("status");
        }

        JSONArray selectList = obj.getJSONArray("selectList");

        if ("未闭关".equals(status)) {
            for (int i = selectList.size() - 1; i >= 0; i--) {
                JSONObject select = selectList.getJSONObject(i);

                String response = Api.get(ApiConstants.API_PRACTICE_ROOM_BEGAIN_PRACTICE, select);

                String result = ReUtil.get("闭关成功，耐力-\\d+", response, 0);
                if (StrUtil.isNotEmpty(result)) {
                    Api.out(I, result);
                    break;
                }

                result = ReUtil.get("正在修炼中", response, 0);
                if (StrUtil.isNotEmpty(result)) {
                    Api.out(I, result);
                    break;
                }
            }

            // 刷新闭关状态
            obj = getInfo();
            status = obj.getStr("status");
        }

//        if (status.contains("后可出关")) {
//            Api.out(I, "尝试免费加速功能");
//        }
    }

    public void auto() {
        execute();
    }

    public static void main(String[] args) {
        new PracticeRoomServiceImpl().execute();
    }
}
